package com.test.bookmark;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Browser;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.hello).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent("android.intent.action.ADD_LINK_TO_BROWSER");
//                intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
//                intent.putExtra("url", "http://m.baidu.com");
//                startActivity(intent);
//                openUrlByOupeng("http://www.baidu.com", MainActivity.this);

//                Browser.saveBookmark(MainActivity.this, "baidu", "http://www.baidu.com");

//                sdpTest();
//                showToast();

                Log.d("XXXXXX", "XX_1_384_808_5_84_9 => " + regTest("XX_1_384_808_5_84_9"));
                Log.d("XXXXXX", "XX_1_384_808_5_84_9 => " + regTest("XX_1_384_808_5_84_9"));
                Log.d("XXXXXX", "xx_1111_122_871 => " + regTest("xx_1111_122_871"));
                Log.d("XXXXXX", "3_1004_1004111_78_1 => " + regTest("3_1004_1004111_78_1"));
                Log.d("XXXXXX", "3_1005_1005111_78_1 => " + regTest("3_1005_1005111_78_1"));
                Log.d("XXXXXX", "3_5510_5510111_78_1 => " + regTest("3_5510_5510111_78_1"));
                Log.d("XXXXXX", "3_1515_1515111_78_1 => " + regTest("3_1515_1515111_78_1"));
                Log.d("XXXXXX", "Oupeng => " + regTest("Oupeng"));
                Log.d("XXXXXX", "Test => " + regTest("Test"));
            }
        });
    }

    private void sdpTest() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("http://imps.tcl-ta.com/stream/sdp/orange_stream_forward.sdp"), "application/sdp");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("url-authorization", "Basic anJkOmpyZA=="); // the Base64 encoding of "jrd:jrd"
//        intent.putExtra("url-authorization", "jrd:jrd"); // user:password does not work either
        startActivity(intent);
    }

    private void showToast() {
        Toast t = new Toast(this);
        t.setText("This is a toast");
        t.show();
    }

    private String regTest(String cid) {
        Pattern p = Pattern.compile("^(XX_|xx_)?\\d+_\\d+");
        Matcher m = p.matcher(cid);
        if (m.find()) {
            cid = m.group();
        }

        return cid;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Open the url by Oupeng browser, if Oupeng browser installed. Otherwise let user choose
     * a browser to open it.
     *
     * @param url the url to open
     * @param context context
     */
    public void openUrlByOupeng(String url, Context context) {
        if (url == null || context == null) {
            return;
        }

        String oupengPackageNames[] = { "com.oupeng.mini.android", "com.oupeng.browser" };
        String oupengPackageName = null;
        for (String package_name : oupengPackageNames) {
            if (isPackageInstalled(context, package_name) && isPackageEnabled(context, package_name)) {
                oupengPackageName = package_name;
                break;
            }
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (oupengPackageName != null) {
            intent.setPackage(oupengPackageName);
        }
        startActivity(intent);
    }

    private PackageInfo getPackageInfo(Context context, String packageName) {
        try {
            return context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
        }
        return null;
    }

    private boolean isPackageInstalled(Context context, String packageName) {
        return getPackageInfo(context, packageName) != null;
    }

    private boolean isPackageEnabled(Context context, String packageName) {
        final int enableStatus = context.getPackageManager().getApplicationEnabledSetting(packageName);
        return PackageManager.COMPONENT_ENABLED_STATE_ENABLED == enableStatus
                || PackageManager.COMPONENT_ENABLED_STATE_DEFAULT == enableStatus;
    }
}
